package dytmp

import (
	"context"
	"fmt"
	"io"
	"log"
	"net/url"
	"os"
	"time"

	"github.com/minio/minio-go/v7"
	"go.uber.org/zap"
)

// 上传对象
func UploadFile(minioClient *minio.Client, bucketName string, objectName string, localFileName string) (string, error) {
	ctx := context.Background()
	src, err1 := os.Open(localFileName)
	fileStat, _ := src.Stat()
	if err1 != nil {
		fmt.Println("file=", fileStat.Name())
		fmt.Println("UploadFile -> file.Open()", err1)
	}
	_, err := minioClient.PutObject(
		ctx,
		bucketName,
		objectName,
		src,
		fileStat.Size(),
		minio.PutObjectOptions{ContentType: "application/octet-stream"})

	if err != nil {
		fmt.Println("MinIO 上传文件失败", zap.Error(err))
		return "", err
	}
	location, err3 := minioClient.GetBucketLocation(ctx, bucketName)
	if err3 != nil {
		fmt.Println("GetBucketLocation", err3)
	}
	fmt.Println(location)
	return fileStat.Name(), nil
}

// 下载对象
func DownFile(minioClient *minio.Client, bucketName string, objectName string, localFileName string) (string, error) {
	ctx := context.Background()
	object, err := minioClient.GetObject(ctx, bucketName, objectName, minio.GetObjectOptions{})
	if err != nil {
		log.Fatalf("download object error " + err.Error())
		return "", err
	}
	localFile, err := os.Create(localFileName)
	if err != nil {
		log.Fatalf("create local object error " + err.Error())
	}
	_, err = io.Copy(localFile, object)
	if err != nil {
		log.Fatalf("write object from object to local file error " + err.Error())
		return "", err
	}

	return objectName, nil
}

// 删除对象
func RemoveFile(minioClient *minio.Client, bucketName string, objectName string) error {
	err := minioClient.RemoveObject(context.Background(), bucketName, objectName, minio.RemoveObjectOptions{})
	if err != nil {
		log.Fatalf("remove object eror " + err.Error())
		return err
	}
	return err
}

// 获取URL
func GetFileURL(minioClient *minio.Client, bucketName string, objectName string, expire time.Duration) (*url.URL, error) {
	// Set request parameters for content-disposition.
	reqParams := make(url.Values)
	// reqParams.Set("response-content-disposition", "attachment;")
	if expire < 0 {
		expire = time.Second * 24 * 60 * 60
	}
	// Generates a presigned url which expires in a day.
	presignedURL, err := minioClient.PresignedGetObject(context.Background(), bucketName, objectName, expire, reqParams)
	if err != nil {
		fmt.Println(err)
		return nil, err
	}
	fmt.Println("Successfully generated presigned URL", presignedURL)
	return presignedURL, nil
}

func main() {
	// MinIO()
	// file, err := os.Open("C:/Users/admin/Documents/GitHub/青训营实践项目/DOUSHENG/tiktok/pkg/minio/test.mp4")
	// if err != nil {
	// 	fmt.Println("打开文件", err)
	// }
	// f_stat, _ := file.Stat()
	// fmt.Println("读取的文件名", file.Name())
	// fmt.Println("f_stat", f_stat.Name())
	// defer file.Close()
	// fileHeader, err := getFileHeader(file)
	// if err != nil {
	// 	fmt.Println("getFileHeader", err)
	// }

	// localFileName := "C:/Users/admin/Documents/GitHub/青训营实践项目/DOUSHENG/tiktok/pkg/minio/test.mp4"
	// upload_res, err := UploadFile(MinIOClient, BucketName, "upload_demo.mp4", localFileName)
	// fmt.Println("upload_res=", upload_res)
	// if err != nil {
	// 	fmt.Println("getFileHeader", err)
	// }

	// 下载对象
	// DownFile(MinIOClient, BucketName, "upload_demo", "./download_demo")

	// 删除对象
	// RemoveFile(MinIOClient, BucketName, "upload_demo")

	// getURL
	// GetFileURL(MinIOClient, BucketName, "upload_demo.mp4", time.Second*10)
}

// func getFileHeader(file *os.File) (*multipart.FileHeader, error) {
// 	// get file size
// 	fileStat, err := file.Stat()
// 	if err != nil {
// 		return nil, err
// 	}

// 	// create *multipart.FileHeader
// 	return &multipart.FileHeader{
// 		Filename: file.Name(),
// 		Size:     fileStat.Size(),
// 	}, nil
// }
